

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/Mine.jpg">
  <link rel="icon" href="/img/Mine.jpg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Chiam">
  <meta name="keywords" content="算法，安全">
  
    <meta name="description" content="『数据库』震惊，某博主为吸引眼球拿出压箱底 SQL 总结，如果你没看那就吃亏了！（超级详细的 SQL 基础，你还不会的话就别学数据库了）这里还有数据库相关的优质文章：快戳我，快戳我 😳 前言：使用 SQL 语言时，要注意 SQL 语言对大小写并不敏感，一般使用大写。所有符号一定是西文标点符号（虽然是常识，但我还是提一嘴）文章有点长，大家要耐心看完呀！👍👍👍👍👍👍 1、模式的定义与删除">
<meta property="og:type" content="article">
<meta property="og:title" content="『数据库』震惊，某博主为吸引眼球拿出压箱底SQL总结，如果你没看那就吃亏了！（超级详细的SQL基础，你还不会的话就别学数据库了）">
<meta property="og:url" content="http://example.com/2023/12/06/%E3%80%8E%E6%95%B0%E6%8D%AE%E5%BA%93%E3%80%8F%E9%9C%87%E6%83%8A%EF%BC%8C%E6%9F%90%E5%8D%9A%E4%B8%BB%E4%B8%BA%E5%90%B8%E5%BC%95%E7%9C%BC%E7%90%83%E6%8B%BF%E5%87%BA%E5%8E%8B%E7%AE%B1%E5%BA%95SQL%E6%80%BB%E7%BB%93%EF%BC%8C%E5%A6%82%E6%9E%9C%E4%BD%A0%E6%B2%A1%E7%9C%8B%E9%82%A3%E5%B0%B1%E5%90%83%E4%BA%8F%E4%BA%86%EF%BC%81%EF%BC%88%E8%B6%85%E7%BA%A7%E8%AF%A6%E7%BB%86%E7%9A%84SQL%E5%9F%BA%E7%A1%80%EF%BC%8C%E4%BD%A0%E8%BF%98%E4%B8%8D%E4%BC%9A%E7%9A%84%E8%AF%9D%E5%B0%B1%E5%88%AB%E5%AD%A6%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BA%86%EF%BC%89/index.html">
<meta property="og:site_name" content="Chiam 的个人主页">
<meta property="og:description" content="『数据库』震惊，某博主为吸引眼球拿出压箱底 SQL 总结，如果你没看那就吃亏了！（超级详细的 SQL 基础，你还不会的话就别学数据库了）这里还有数据库相关的优质文章：快戳我，快戳我 😳 前言：使用 SQL 语言时，要注意 SQL 语言对大小写并不敏感，一般使用大写。所有符号一定是西文标点符号（虽然是常识，但我还是提一嘴）文章有点长，大家要耐心看完呀！👍👍👍👍👍👍 1、模式的定义与删除">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200315172448829.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzYyNzExOA==,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200315164416255.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzYyNzExOA==,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200315190124498.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzYyNzExOA==,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2023-12-05T16:11:43.893Z">
<meta property="article:modified_time" content="2023-12-05T16:18:16.224Z">
<meta property="article:author" content="Chiam">
<meta property="article:tag" content="算法，安全">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20200315172448829.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzYyNzExOA==,size_16,color_FFFFFF,t_70">
  
  
  
  <title>『数据库』震惊，某博主为吸引眼球拿出压箱底SQL总结，如果你没看那就吃亏了！（超级详细的SQL基础，你还不会的话就别学数据库了） - Chiam 的个人主页</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/css/custom.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.5-a","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":"❡"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":2},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Chiam&#39;s Blogs</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                
                <span>友链</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="『数据库』震惊，某博主为吸引眼球拿出压箱底SQL总结，如果你没看那就吃亏了！（超级详细的SQL基础，你还不会的话就别学数据库了）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-12-06 00:11" pubdate>
          2023年12月6日 凌晨
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          9.5k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          80 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">『数据库』震惊，某博主为吸引眼球拿出压箱底SQL总结，如果你没看那就吃亏了！（超级详细的SQL基础，你还不会的话就别学数据库了）</h1>
            
            
              <div class="markdown-body">
                
                <h1 id="『数据库』震惊，某博主为吸引眼球拿出压箱底-SQL-总结，如果你没看那就吃亏了！（超级详细的-SQL-基础，你还不会的话就别学数据库了）"><a href="#『数据库』震惊，某博主为吸引眼球拿出压箱底-SQL-总结，如果你没看那就吃亏了！（超级详细的-SQL-基础，你还不会的话就别学数据库了）" class="headerlink" title="『数据库』震惊，某博主为吸引眼球拿出压箱底 SQL 总结，如果你没看那就吃亏了！（超级详细的 SQL 基础，你还不会的话就别学数据库了）"></a>『数据库』震惊，某博主为吸引眼球拿出压箱底 SQL 总结，如果你没看那就吃亏了！（超级详细的 SQL 基础，你还不会的话就别学数据库了）</h1><p>这里还有数据库相关的优质文章：<a target="_blank" rel="noopener" href="https://lunatic.blog.csdn.net/article/details/105027257">快戳我，快戳我 😳</a></p>
<h4 id="前言："><a href="#前言：" class="headerlink" title="前言："></a>前言：</h4><p>使用 SQL 语言时，要注意 SQL 语言对大小写并不敏感，一般使用大写。所有符号一定是西文标点符号（虽然是常识，但我还是提一嘴）<br>文章有点长，大家要耐心看完呀！👍👍👍👍👍👍<br><img src="https://img-blog.csdnimg.cn/20200315172448829.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzYyNzExOA==,size_16,color_FFFFFF,t_70" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h4 id="1、模式的定义与删除"><a href="#1、模式的定义与删除" class="headerlink" title="1、模式的定义与删除"></a>1、模式的定义与删除</h4><h5 id="1-1-定义模式"><a href="#1-1-定义模式" class="headerlink" title="1.1 定义模式"></a>1.1 定义模式</h5><p>在 CREATE SCHEMA 中可以接受 CREATE TABLE，CREATE VIEW 和 GRANT 子句，语句如下：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> SCHEMA <span class="hljs-operator">&lt;</span>模式名<span class="hljs-operator">&gt;</span> <span class="hljs-keyword">AUTHORIZATION</span> <span class="hljs-operator">&lt;</span>用户名<span class="hljs-operator">&gt;</span>[<span class="hljs-operator">&lt;</span>表定义子句<span class="hljs-operator">&gt;</span><span class="hljs-operator">|</span><span class="hljs-operator">&lt;</span>视图定义子句<span class="hljs-operator">&gt;</span><span class="hljs-operator">|</span><span class="hljs-operator">&lt;</span>授权定义子句<span class="hljs-operator">&gt;</span>]<br></code></pre></td></tr></table></figure>

<p>注：如果没有指定&lt;模式名&gt;，那么&lt;模式名&gt;隐含为&lt;用户名&gt; 。</p>
<p><strong>例子：</strong> 为用户 ZHANG 创建了一个模式 TEST，并且在其中定义一个表 TAB1<br>其中这个 TAB1 的表有 5 列，第一列为短整型，第二列为整型，第三列为定长字符串，第四列为定点数，第五列跟第四列一样。稍微了解一下，后面将会详细的介绍，表的定义。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> SCHEMA TEST <span class="hljs-keyword">AUTHORIZATION</span> ZHANG<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> TAB1 (<br>  COL1 <span class="hljs-type">SMALLINT</span>,<br>  COL2 <span class="hljs-type">INT</span>,<br>  COL3 <span class="hljs-type">CHAR</span>(<span class="hljs-number">20</span>),<br>  COL4 <span class="hljs-type">NUMERIC</span>(<span class="hljs-number">10</span>,<span class="hljs-number">3</span>),<br>  COL5 <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">5</span>,<span class="hljs-number">2</span>)<br>);<br></code></pre></td></tr></table></figure>

<h5 id="1-2-删除模式"><a href="#1-2-删除模式" class="headerlink" title="1.2 删除模式"></a>1.2 删除模式</h5><p>语句格式：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DROP</span> SCHEMA <span class="hljs-operator">&lt;</span>模式名<span class="hljs-operator">&gt;</span> <span class="hljs-operator">&lt;</span>CASCADE<span class="hljs-operator">|</span>RESTRICT<span class="hljs-operator">&gt;</span><br><br></code></pre></td></tr></table></figure>

<ol>
<li>CASCADE（级联）连带将模式中的数据库对象都删除<br>删除模式的同时把该模式中所有的数据库对象全部删除 ，即 所有依赖此模式的对象都会被删除。</li>
<li>RESTRICT（限制）有下属数据库对象时，不删除<br>如果该模式中定义了下属的数据库对象（如表、视图等），则拒绝该删除语句的执行。 仅当该模式中没有任何下属的对象时才能执行。</li>
</ol>
<p><strong>例子：</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">--删除模式ZHANG， 则删除失败，因为模式中定义了TAB1</span><br><span class="hljs-keyword">DROP</span> SCHEMA ZHANG RESTRICT;<br></code></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">--删除模式ZHANG， 同时该模式中定义的表TAB1也被删除</span><br><span class="hljs-keyword">DROP</span> SCHEMA ZHANG CASCADE;<br></code></pre></td></tr></table></figure>

<h4 id="2、基本表的定义、删除与修改"><a href="#2、基本表的定义、删除与修改" class="headerlink" title="2、基本表的定义、删除与修改"></a>2、基本表的定义、删除与修改</h4><h5 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h5><table>
<thead>
<tr>
<th>数据类型</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>CHAR(n)</td>
<td>长度为 n 的定长字符串，也可写作 CHARACTER(n）</td>
</tr>
<tr>
<td>VARCHAR(n)</td>
<td>最大长度为 n 的变长字符串，也可写作 CHARACTERVARCHAR(n)</td>
</tr>
<tr>
<td>CLOB</td>
<td>字符串大对象</td>
</tr>
<tr>
<td>BLOB</td>
<td>二进制大对象</td>
</tr>
<tr>
<td>INT</td>
<td>长整数，4 字节（也可以写作 INTEGER）</td>
</tr>
<tr>
<td>SMALLINT</td>
<td>短整数，2 字节</td>
</tr>
<tr>
<td>BIGINT</td>
<td>大整数，8 字节</td>
</tr>
<tr>
<td>NUMERIC(p，d)</td>
<td>定点数，由 p 位数字（不包括符号、小数点）组成，小数后面有 d 位数字</td>
</tr>
<tr>
<td>DECIMAL(p,d)</td>
<td>含义和 NUMBER 一样，也可以写作 DEC（p，d)</td>
</tr>
<tr>
<td>REAL</td>
<td>取决于机器单精度的浮点数</td>
</tr>
<tr>
<td>DOUBLE PRECSION</td>
<td>取决于机器精度的双精度浮点数</td>
</tr>
<tr>
<td>FLOAT(n)</td>
<td>浮点数，精度至少为 n 位数字</td>
</tr>
<tr>
<td>BOOLEAN</td>
<td>布尔逻辑变量</td>
</tr>
<tr>
<td>DATE</td>
<td>日期，包含年、月、日，格式为 YYYY-MM-DD</td>
</tr>
<tr>
<td>TIME</td>
<td>时间，包含一日的时、分、秒，格式为 HH:MM:SS</td>
</tr>
</tbody></table>
<p>其他的</p>
<table>
<thead>
<tr>
<th>数据类型</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>TIMES TAMP</td>
<td>时间戳类型</td>
</tr>
<tr>
<td>INTERVAL</td>
<td>时间间隔类型</td>
</tr>
</tbody></table>
<h5 id="2-1-定义表"><a href="#2-1-定义表" class="headerlink" title="2.1 定义表"></a>2.1 定义表</h5><p><strong>语句格式：</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-operator">&lt;</span>表名<span class="hljs-operator">&gt;</span><br>(<span class="hljs-operator">&lt;</span>列名<span class="hljs-operator">&gt;</span> <span class="hljs-operator">&lt;</span>数据类型<span class="hljs-operator">&gt;</span>[ <span class="hljs-operator">&lt;</span>列级完整性约束条件<span class="hljs-operator">&gt;</span> ]<br>[,<span class="hljs-operator">&lt;</span>列名<span class="hljs-operator">&gt;</span> <span class="hljs-operator">&lt;</span>数据类型<span class="hljs-operator">&gt;</span>[ <span class="hljs-operator">&lt;</span>列级完整性约束条件<span class="hljs-operator">&gt;</span>] ]<br>......<br>[,<span class="hljs-operator">&lt;</span>表级完整性约束条件<span class="hljs-operator">&gt;</span> ] );<br></code></pre></td></tr></table></figure>

<p><strong>注：</strong> 如果完整性约束条件涉及到该表的多个属性列，则必须定义在表级上，否则既可以定义在列级也可以定义在表级。</p>
<p><strong>例子：</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-comment">--建立一个学生选课表SC</span><br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span>  SC<br>  ( Sno  <span class="hljs-type">CHAR</span>(<span class="hljs-number">9</span>),<br>    Cno  <span class="hljs-type">CHAR</span>(<span class="hljs-number">4</span>) <span class="hljs-keyword">UNIQUE</span>, <span class="hljs-comment">--只涉及一个属性，可以定义为列级完整性约束条件</span><br>    Grade  <span class="hljs-type">SMALLINT</span>，<br><br>    <span class="hljs-keyword">PRIMARY</span> KEY (Sno,Cno), <span class="hljs-comment">--主码由</span><br>    <span class="hljs-comment">/*约束条件涉及到该表的两个属性列，必须作为表级完整性进行定义*/</span><br>    <span class="hljs-keyword">FOREIGN</span> KEY (Sno) <span class="hljs-keyword">REFERENCES</span> Student(Sno), <span class="hljs-comment">/* 表级完整性约束条件，Sno是外码，被参照表是Student */</span><br>    <span class="hljs-keyword">FOREIGN</span> KEY (Cno)<span class="hljs-keyword">REFERENCES</span> Course(Cno) <span class="hljs-comment">/* 表级完整性约束条件， Cno是外码，被参照表是Course*/</span><br>  );<br></code></pre></td></tr></table></figure>

<p><strong>常见的五种完整性约束：</strong></p>
<p>1.NOT NULL（非空)约束： 只用于定义列约束。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql">CREAT　<span class="hljs-keyword">TABLE</span>　 Employee<br>(<br>   Emp_id <span class="hljs-type">int</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">null</span>,<br>    Emp_name <span class="hljs-type">varchar</span>(<span class="hljs-number">10</span>) <span class="hljs-keyword">not</span> <span class="hljs-keyword">null</span>,<br>    EMP_address <span class="hljs-type">varchar</span>(<span class="hljs-number">40</span>) ,<br>)<br></code></pre></td></tr></table></figure>

<p>创建之后，如果往表 Employee 表中非空约束中插入空值将会出错。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span>  Employee <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">1</span>,<span class="hljs-keyword">NULL</span>,<span class="hljs-string">&#x27;neimeng&#x27;</span>)<br><span class="hljs-comment">/*</span><br><span class="hljs-comment">报错</span><br><span class="hljs-comment">Cannot insert the value NULL into column &#x27;emp_name&#x27;, table &#x27;Student.dbo.Employee&#x27;;</span><br><span class="hljs-comment">column does not allow nulls. INSERT fails.</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<p>2.UNIQUE(惟一)约束：用于指明创建惟一约束的列上的取值必须惟一。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql">CREAT　<span class="hljs-keyword">TABLE</span>　 Employee<br>(<br>    Emp_id <span class="hljs-type">int</span> <span class="hljs-keyword">UNIQUE</span>,<br>    Emp_name <span class="hljs-type">varchar</span>(<span class="hljs-number">10</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>    EMP_address <span class="hljs-type">varchar</span>(<span class="hljs-number">40</span>) ,<br>)<br></code></pre></td></tr></table></figure>

<p>如果向 Employee 插入数据时，如果两条记录的 Emp_id 不惟一，则会出现错误。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span>  Employee <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">123</span>,<span class="hljs-string">&#x27;xiaoming&#x27;</span>,<span class="hljs-string">&#x27;neimeng&#x27;</span>)<br><span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span>  Employee <span class="hljs-keyword">VALUES</span>(<span class="hljs-number">123</span>,<span class="hljs-string">&#x27;xiaoWang&#x27;</span>,<span class="hljs-string">&#x27;hebei&#x27;</span>)<br> <span class="hljs-comment">/*</span><br><span class="hljs-comment"> Violation of UNIQUE KEY constraint &#x27;UQ__Employee__0051DEAE8&#x27;.</span><br><span class="hljs-comment"> Cannot insert duplicate key in object &#x27;dbo.Employee&#x27;.</span><br><span class="hljs-comment"> */</span><br></code></pre></td></tr></table></figure>

<p><strong>除了在定义列时添加 UNIQUE 约束外，也可以将 unique 约束作为表约束添加。即把它作为表定义的元素。</strong><br>语法如下：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql">[<span class="hljs-keyword">CONSTRAINT</span> constraint_name] <span class="hljs-keyword">UNIQUE</span> (COL1,COL2,.....)<br></code></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs sql">CREAT　<span class="hljs-keyword">TABLE</span>　 Employee<br>(<br>   Emp_id <span class="hljs-type">int</span> <span class="hljs-keyword">UNIQUE</span>,<br>    Emp_name <span class="hljs-type">varchar</span>(<span class="hljs-number">10</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>    EMP_address <span class="hljs-type">varchar</span>(<span class="hljs-number">40</span>) ,<br><span class="hljs-keyword">constraint</span> p_uniq <span class="hljs-keyword">unique</span>(Emp_id )<br>)<br></code></pre></td></tr></table></figure>

<p><strong>3.Primary KEY(主键)约束：</strong><br>用于定义基本表的主键，起惟一标识作用，其值不能为 null,也不能重复，以此来保证实体的完整性。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sql">CREAT　<span class="hljs-keyword">TABLE</span>　 Employee<br>(<br>    Emp_id <span class="hljs-type">int</span> <span class="hljs-keyword">Primary</span> KEY,<br>    Emp_name <span class="hljs-type">varchar</span>(<span class="hljs-number">10</span>) <span class="hljs-keyword">NOT</span> <span class="hljs-keyword">NULL</span>,<br>    EMP_address <span class="hljs-type">varchar</span>(<span class="hljs-number">40</span>) ,<br>)<br></code></pre></td></tr></table></figure>

<p>如果向 Employee 表插入的 Emp_id 重复了或者插入时 Emp_id 为 NULL 值，则会出错。<br>可以在创建表时，创建主键约束，也可创建表完成以后，创建主键。</p>
<p><strong>例如：</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">alter</span> <span class="hljs-keyword">table</span> EmployeeInfo<br><span class="hljs-keyword">add</span> <span class="hljs-keyword">constraint</span> e_prim <span class="hljs-keyword">primary</span> key(emp_id)<br></code></pre></td></tr></table></figure>

<p><strong>PRIMARY KEY 与 UNQIUE 的区别：</strong> 1.在一个表中，只能定义一个 PRIMARY KEY 约束，但可定义多个 UNQIUE 约束。</p>
<p>2.对于指定为 primary key 的一个列或多个列的组合，其中任何一个列都不能出现空值，而对于 unique 所约束的惟一键，则允许为 null,只是 null 值最多有一个。</p>
<p>** 4.FOREGIN KEY(外键)约束：**</p>
<p>定义了一个表中数据与另一个表中的数据的联系。<br>FOREGIN KEY 约束指定某一个列或一组列作为外部键，其中包含外部键的表称为子表，包含外部键所引用的主键的表称为父表。系统保证，表在外部键上的取值要么是父表中某一主键，要么取空值，以此保证两个表之间的连接，确保了实体的参照完整性。</p>
<p>通俗的说，外键是对另一个表中主键的引用。 如果公共关键字在一个关系中是主关键字，那么这个公共关键字被称为另一个关系的外键（FK）。即，当一张二维表（如表 A）的主关键字被包含在另一张二维表（如表 B）中时，A 表中的主关键字便成为 B 表的外关键字。</p>
<h5 id="2-2-定义表的所属模式"><a href="#2-2-定义表的所属模式" class="headerlink" title="2.2 定义表的所属模式"></a>2.2 定义表的所属模式</h5><p><strong>方法一：在表名中明显地给出模式名</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">Create</span> <span class="hljs-keyword">table</span>&quot;S-T&quot;.Student(......);     <span class="hljs-comment">/*模式名为 S-T*/</span><br><span class="hljs-keyword">Create</span> <span class="hljs-keyword">table</span> &quot;S-T&quot;.Cource(......);<br><span class="hljs-keyword">Create</span> <span class="hljs-keyword">table</span> &quot;S-T&quot;.SC(......);<br></code></pre></td></tr></table></figure>

<p><strong>方法二：在创建模式语句中同时创建表</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> SCHEMA TEST <span class="hljs-keyword">AUTHORIZATION</span> ZHANG<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> TAB1 (<br>  COL1 <span class="hljs-type">SMALLINT</span>,<br>  COL2 <span class="hljs-type">INT</span>,<br>  COL3 <span class="hljs-type">CHAR</span>(<span class="hljs-number">20</span>),<br>  COL4 <span class="hljs-type">NUMERIC</span>(<span class="hljs-number">10</span>,<span class="hljs-number">3</span>),<br>  COL5 <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">5</span>,<span class="hljs-number">2</span>)<br>);<br></code></pre></td></tr></table></figure>

<p><strong>方法三：设置所属的模式</strong></p>
<ul>
<li>创建基本表（其他数据库对象也一样）时，若没有指定模式，系统根据搜索路径来确定该对象所属的模式</li>
<li>关系数据库管理系统会使用模式列表中第一个存在的模式作为数据库对象的模式名</li>
<li>若搜索路径中的模式名都不存在，系统将给出错误<br>显示当前的搜索路径： SHOW search_path; 搜索路径的当前默认值是：$user， PUBLIC</li>
</ul>
<p>设置搜索路径，然后定义基本表：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SET</span> search_path <span class="hljs-keyword">TO</span> &quot;S-T&quot;,PUBLIC; <span class="hljs-operator">/</span><span class="hljs-operator">/</span>设置搜索路径<br><span class="hljs-keyword">Create</span> <span class="hljs-keyword">table</span> Student(......); <span class="hljs-operator">/</span><span class="hljs-operator">/</span>定义基本表<br></code></pre></td></tr></table></figure>

<p>结果建立了 S-T.Student 基本表</p>
<h5 id="2-3-修改表"><a href="#2-3-修改表" class="headerlink" title="2.3 修改表"></a>2.3 修改表</h5><p>语句格式：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> <span class="hljs-operator">&lt;</span>表名<span class="hljs-operator">&gt;</span><br>[<span class="hljs-keyword">ADD</span> <span class="hljs-operator">&lt;</span>新列名<span class="hljs-operator">&gt;</span><span class="hljs-operator">&lt;</span>数据类型<span class="hljs-operator">&gt;</span>[完整性约束]]<br>[<span class="hljs-keyword">DROP</span> <span class="hljs-operator">&lt;</span>完整性约束名<span class="hljs-operator">&gt;</span>]<br>[<span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">COLUMN</span> <span class="hljs-operator">&lt;</span>列名<span class="hljs-operator">&gt;</span> <span class="hljs-operator">&lt;</span>数据类型<span class="hljs-operator">&gt;</span>];<br></code></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th>指令</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>ADD</td>
<td>子句用于增加新列、新的列级完整性约束条件和新的表级完整性约束条件</td>
</tr>
<tr>
<td>DROP COLUMN</td>
<td>子句用于删除表中的列<br><br>如果指定了 CASCADE 短语，则自动删除引用了该列的其他对象<br>如果指定了 RESTRICT 短语，则如果该列被其他对象引用，关系数据库管理系统将拒绝删除该列</td>
</tr>
<tr>
<td>DROP CONSTRAINT</td>
<td>子句用于删除指定的完整性约束条件</td>
</tr>
<tr>
<td>ALTER COLUMN</td>
<td>子句用于修改原有的列定义，包括修改列名和数据类型</td>
</tr>
</tbody></table>
<p>例子：<br>向 Course 表增加“开始时间”列，将其数据类型为日期型</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> Course <span class="hljs-keyword">ADD</span> C_start <span class="hljs-type">DATE</span>;<br></code></pre></td></tr></table></figure>

<p>将 Ccredit 分数的数据类型由短整数改为整数</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> Course <span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">COLUMN</span> Ccredit <span class="hljs-type">INT</span> ;<br></code></pre></td></tr></table></figure>

<p>增加课程名称必须取唯一值得约束条件</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> Course <span class="hljs-keyword">ADD</span> <span class="hljs-keyword">UNIQUE</span>(Cname);<br></code></pre></td></tr></table></figure>

<h5 id="2-4-删除表"><a href="#2-4-删除表" class="headerlink" title="2.4 删除表"></a>2.4 删除表</h5><p>语句格式：</p>
<p>DROP TABLE &lt;表名&gt;［RESTRICT| CASCADE］;</p>
<ol>
<li>RESTRICT：删除表是有限制的<br>欲删除的基本表不能被其他表的约束所引用；<br>如果存在依赖该表的对象，则此表不能被删除</li>
<li>CASCADE：删除该表没有限制<br>在删除基本表的同时，相关的依赖对象（表定义、数据、索引、视图、触发器等）一起删除</li>
</ol>
<h4 id="3-索引的建立与删除"><a href="#3-索引的建立与删除" class="headerlink" title="3.索引的建立与删除"></a>3.索引的建立与删除</h4><ul>
<li>建立索引的目的：加快查询速度</li>
<li>谁可以建立索引<br>DBA 或 表的属主（即建立表的人）<br>DBMS 一般会自动建立以下列上的索引<br><code>PRIMARY</code><br><code> KEY  UNIQUE </code></li>
<li>索引的维护：<br>DBMS 自动完成</li>
<li>索引的使用<br>DBMS 自动选择是否使用索引以及使用哪些索引</li>
<li>RDBMS 中索引一般采用 B+树、HASH 索引来实现<br>B+树索引具有动态平衡的优点<br>HASH 索引具有查找速度快的特点</li>
<li>采用 B+树，还是 HASH 索引 则由具体的 RDBMS 来决定</li>
<li>索引是关系数据库的内部实现技术，属于内模式的范畴</li>
<li>CREATE INDEX 语句定义索引时，可以定义索引是唯一索引、非唯一索引或聚簇索引</li>
</ul>
<h5 id="3-1-创建索引"><a href="#3-1-创建索引" class="headerlink" title="3.1 创建索引"></a>3.1 创建索引</h5><p>语句格式：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> [<span class="hljs-keyword">UNIQUE</span>] [CLUSTER] INDEX <span class="hljs-operator">&lt;</span>索引名<span class="hljs-operator">&gt;</span><br><span class="hljs-keyword">ON</span> <span class="hljs-operator">&lt;</span>表名<span class="hljs-operator">&gt;</span>(<span class="hljs-operator">&lt;</span>列名<span class="hljs-operator">&gt;</span>[<span class="hljs-operator">&lt;</span>次序<span class="hljs-operator">&gt;</span>][,<span class="hljs-operator">&lt;</span>列名<span class="hljs-operator">&gt;</span>[<span class="hljs-operator">&lt;</span>次序<span class="hljs-operator">&gt;</span>] ]…);<br></code></pre></td></tr></table></figure>

<ul>
<li><p>&lt;表名&gt;：要建索引的基本表的名字</p>
</li>
<li><p>索引：可以建立在该表的一列或多列上，各列名之间用逗号分隔</p>
</li>
<li><p>&lt;次序&gt;：指定索引值的排列次序，升序：ASC，降序：DESC。缺省值：ASC</p>
</li>
<li><p>UNIQUE：此索引的每一个索引值只对应唯一的数据记录</p>
</li>
<li><p>CLUSTER：表示要建立的索引是聚簇索引</p>
</li>
</ul>
<p><strong>例子：</strong><br>为学生-课程数据库中的 Student，Course，SC 三个表建立索引</p>
<p>Student 表按学号升序建唯一索引<br>Course 表按课程号升序建唯一索引<br>SC 表按学号升序和课程号降序建唯一索引</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">UNIQUE</span> INDEX  Stusno <span class="hljs-keyword">ON</span> Student(Sno); <span class="hljs-operator">/</span><span class="hljs-operator">/</span>默认升序<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">UNIQUE</span> INDEX  Coucno <span class="hljs-keyword">ON</span> Course(Cno);<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">UNIQUE</span> INDEX  SCno <span class="hljs-keyword">ON</span> SC(Sno <span class="hljs-keyword">ASC</span>,Cno <span class="hljs-keyword">DESC</span>);<br></code></pre></td></tr></table></figure>

<p><strong>聚簇索引：</strong></p>
<p>将数据存储与索引放到了一块，找到索引也就找到了数据</p>
<p><strong>非聚簇索引：</strong></p>
<p>将数据存储于索引分开结构，索引结构的叶子节点指向了数据的对应行，myisam 通过 key_buffer 把索引先缓存到内存中，当需要访问数据时（通过索引访问数据），在内存中直接搜索索引，然后通过索引找到磁盘相应数据，这也就是为什么索引不在 key buffer 命中时，速度慢的原因。</p>
<p><strong>注：如果索引建的不对速度会大受影响</strong><br><img src="https://img-blog.csdnimg.cn/20200315164416255.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzYyNzExOA==,size_16,color_FFFFFF,t_70" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h5 id="3-2-修改索引"><a href="#3-2-修改索引" class="headerlink" title="3.2 修改索引"></a>3.2 修改索引</h5><p>语句格式：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">ALTER</span> INDEX <span class="hljs-operator">&lt;</span>旧索引名<span class="hljs-operator">&gt;</span> RENAME <span class="hljs-keyword">TO</span> <span class="hljs-operator">&lt;</span>新索引名<span class="hljs-operator">&gt;</span><br></code></pre></td></tr></table></figure>

<p><strong>例子：</strong><br>将 SC 表的 SCno 索引名改为 SCSno</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">ALTER</span> INDEX SCno RENAME <span class="hljs-keyword">TO</span> SCSno;<br></code></pre></td></tr></table></figure>

<h5 id="3-3-删除索引"><a href="#3-3-删除索引" class="headerlink" title="3.3 删除索引"></a>3.3 删除索引</h5><p>删除索引时，系统会从数据字典中删去有关该索引的描述<br>语句格式：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DROP</span> INDEX <span class="hljs-operator">&lt;</span>索引名<span class="hljs-operator">&gt;</span>;<br></code></pre></td></tr></table></figure>

<p><strong>例子：</strong><br>删除 Student 表的 Stusname 索引</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DROP</span> INDEX Stusname;<br></code></pre></td></tr></table></figure>

<h4 id="4-视图的创建与删除"><a href="#4-视图的创建与删除" class="headerlink" title="4.视图的创建与删除"></a>4.视图的创建与删除</h4><h5 id="4-1-创建视图"><a href="#4-1-创建视图" class="headerlink" title="4.1 创建视图"></a>4.1 创建视图</h5><p>语句格式</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span>  <span class="hljs-keyword">VIEW</span>  <span class="hljs-operator">&lt;</span>视图名<span class="hljs-operator">&gt;</span>  [(<span class="hljs-operator">&lt;</span>列名<span class="hljs-operator">&gt;</span>  [,<span class="hljs-operator">&lt;</span>列名<span class="hljs-operator">&gt;</span>]…)]<br><span class="hljs-keyword">AS</span>  <span class="hljs-operator">&lt;</span>子查询<span class="hljs-operator">&gt;</span><br>[<span class="hljs-keyword">WITH</span>  <span class="hljs-keyword">CHECK</span>  OPTION];<br></code></pre></td></tr></table></figure>

<ol>
<li>子查询可以是任意的 SELECT 语句，是否可以含有 ORDER BY 子句和 DISTINCT 短语，则决定具体系统的实现</li>
<li>WITH CHECK OPTION：对视图进行 UPDATE，INSERT 和 DELETE 操作时要保证更新、插入或删除的行满足视图定义中的谓词条件（即子查询中的条件表达式）</li>
</ol>
<p><strong>注：</strong></p>
<ol>
<li>修改基表的结构后，可能导致表与视图的映象关系被破坏，从而导致该视图不能正确工作</li>
<li>组成视图的属性列名：全部省略或全部指定</li>
<li>子查询不允许含有 ORDER BY 子句和 DISTINCT 短语</li>
<li>全部省略或全部指定<br>省略:由子查询中 SELECT 目标列中的诸字段组成<br>明确指定视图的所有列名: - 某个目标列是集函数或列表达式 - 目标列为 * - 多表连接时选出了几个同名列作为视图的字段 - 需要在视图中为某个列启用新的更合适的名字</li>
</ol>
<p>例 1：建立信息系学生的视图</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> IS_Student<br><span class="hljs-keyword">AS</span> <span class="hljs-keyword">SELECT</span> Sno,Sname,Sage <span class="hljs-keyword">FROM</span> Student <span class="hljs-keyword">WHERE</span>  Sdept<span class="hljs-operator">=</span> <span class="hljs-string">&#x27;IS&#x27;</span>;<br></code></pre></td></tr></table></figure>

<p>例 2：建立信息系学生的视图，并要求进行修改和插入操作时仍需保证该视图只有信息系的学生</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> IS_Student<br><span class="hljs-keyword">AS</span> <span class="hljs-keyword">SELECT</span> Sno,Sname,Sage <span class="hljs-keyword">FROM</span>  Student <span class="hljs-keyword">WHERE</span>  Sdept<span class="hljs-operator">=</span> <span class="hljs-string">&#x27;IS&#x27;</span><br><span class="hljs-keyword">WITH</span> <span class="hljs-keyword">CHECK</span> OPTION;<br></code></pre></td></tr></table></figure>

<p>透过视图进行增删改操作时，不得破坏视图定义中的谓词条件（即子查询中的条件表达式）</p>
<p><strong>对 IS_Student 视图的更新操作：</strong></p>
<ul>
<li>修改操作：自动加上 Sdept&#x3D; ‘IS’的条件</li>
<li>删除操作：自动加上 Sdept&#x3D; ‘IS’的条件</li>
<li>插入操作：自动检查 Sdept 属性值是否为’IS’</li>
<li>如果不是，则拒绝该插入操作</li>
<li>如果没有提供 Sdept 属性值，则自动定义 Sdept 为’IS’</li>
</ul>
<h6 id="4-1-1-基于多个基表的视图"><a href="#4-1-1-基于多个基表的视图" class="headerlink" title="4.1.1 基于多个基表的视图"></a>4.1.1 基于多个基表的视图</h6><p>例 3：建立信息系选修了 1 号课程的学生的视图（包括学号、姓名、成绩）</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> IS_S1(Sno,Sname,Grade)<br><span class="hljs-keyword">AS</span> <span class="hljs-keyword">SELECT</span> Student.Sno,Sname,Grade <span class="hljs-keyword">FROM</span>  Student,SC<br><span class="hljs-keyword">WHERE</span>  Sdept<span class="hljs-operator">=</span> <span class="hljs-string">&#x27;IS&#x27;</span> <span class="hljs-keyword">AND</span> Student.Sno<span class="hljs-operator">=</span>SC.Sno <span class="hljs-keyword">AND</span> SC.Cno<span class="hljs-operator">=</span> <span class="hljs-string">&#x27;1&#x27;</span>;<br></code></pre></td></tr></table></figure>

<h6 id="4-1-2-基于视图的视图"><a href="#4-1-2-基于视图的视图" class="headerlink" title="4.1.2 基于视图的视图"></a>4.1.2 基于视图的视图</h6><p>例 4：建立信息系选修了 1 号课程且成绩在 90 分以上的学生的视图</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-operator">/</span><span class="hljs-operator">/</span> IS_S2 基于视图IS_s1：<span class="hljs-keyword">FROM</span>  IS_S1<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> IS_S2<br><span class="hljs-keyword">AS</span> <span class="hljs-keyword">SELECT</span> Sno,Sname,Grade <span class="hljs-keyword">FROM</span>  IS_S1  <span class="hljs-keyword">WHERE</span>  Grade<span class="hljs-operator">&gt;=</span><span class="hljs-number">90</span>;<br></code></pre></td></tr></table></figure>

<h6 id="4-1-3-带表达式的视图"><a href="#4-1-3-带表达式的视图" class="headerlink" title="4.1.3 带表达式的视图"></a>4.1.3 带表达式的视图</h6><p>例 5：定义一个反映学生出生年份的视图</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-operator">/</span><span class="hljs-operator">/</span> 表达式：<span class="hljs-number">2014</span><span class="hljs-operator">-</span>Sage<br><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> BT_S(Sno,Sname,Sbirth)<br><span class="hljs-keyword">AS</span> <span class="hljs-keyword">SELECT</span> Sno,Sname,<span class="hljs-number">2014</span><span class="hljs-operator">-</span>Sage <span class="hljs-keyword">FROM</span>  Student;<br></code></pre></td></tr></table></figure>

<h6 id="4-1-4-分组视图"><a href="#4-1-4-分组视图" class="headerlink" title="4.1.4 分组视图"></a>4.1.4 分组视图</h6><p>例 6：将学生的学号及平均成绩定义为一个视图</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-operator">/</span><span class="hljs-operator">/</span> 分组：<span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span><br>CREAT  <span class="hljs-keyword">VIEW</span> S_G(Sno,Gavg)<br><span class="hljs-keyword">AS</span> <span class="hljs-keyword">SELECT</span> Sno,<span class="hljs-built_in">AVG</span>(Grade) <span class="hljs-keyword">FROM</span> SC <span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> Sno;<br></code></pre></td></tr></table></figure>

<h5 id="4-2-删除视图"><a href="#4-2-删除视图" class="headerlink" title="4.2 删除视图"></a>4.2 删除视图</h5><p>语句的格式：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DROP</span>  <span class="hljs-keyword">VIEW</span>  <span class="hljs-operator">&lt;</span>视图名<span class="hljs-operator">&gt;</span>[CASCADE];<br></code></pre></td></tr></table></figure>

<ul>
<li>该语句从数据字典中删除指定的视图定义</li>
<li>如果该视图上还导出了其他视图，使用 CASCADE 级联删除语句，把该视图和由它导出的所有视图一起删除</li>
<li>删除基表时，由该基表导出的所有视图定义都必须显式地使用 DROP VIEW 语句删除</li>
</ul>
<p><strong>例子：</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> IS_S2<br><span class="hljs-keyword">AS</span> <span class="hljs-keyword">SELECT</span> Sno,Sname,Grade <span class="hljs-keyword">FROM</span>  IS_S1  <span class="hljs-keyword">WHERE</span>  Grade<span class="hljs-operator">&gt;=</span><span class="hljs-number">90</span>;<br><br><span class="hljs-comment">--删除视图BT_S</span><br><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> BT_S；<br><br><span class="hljs-comment">--删除视图IS_S1：</span><br><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> IS_S1；<span class="hljs-comment">--拒绝执行</span><br><br><span class="hljs-comment">--级联删除：</span><br><span class="hljs-keyword">DROP</span> <span class="hljs-keyword">VIEW</span> IS_S1 CASCADE;<br><br></code></pre></td></tr></table></figure>

<h5 id="4-3-查询视图"><a href="#4-3-查询视图" class="headerlink" title="4.3 查询视图"></a>4.3 查询视图</h5><p><strong>用户角度：</strong> 查询视图与查询基本表相同<br><strong>RDBMS 实现视图查询的方法：</strong><br>视图消解法（View Resolution） - 进行有效性检查 - 转换成等价的对基本表的查询 - 执行修正后的查询</p>
<p><strong>例子：</strong><br>在信息系学生的视图中找出年龄小于 20 岁的学生</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> IS_Student<br><span class="hljs-keyword">AS</span> <span class="hljs-keyword">SELECT</span> Sno,Sname,Sage <span class="hljs-keyword">FROM</span> Student <span class="hljs-keyword">WHERE</span>  Sdept<span class="hljs-operator">=</span> <span class="hljs-string">&#x27;IS&#x27;</span>;<br><br><span class="hljs-keyword">SELECT</span> Sno,Sage <span class="hljs-keyword">FROM</span> IS_Student <span class="hljs-keyword">WHERE</span> Sage<span class="hljs-operator">&lt;</span><span class="hljs-number">20</span>;<br></code></pre></td></tr></table></figure>

<p>视图消解转换后的查询语句为：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span>  Sno，Sage<br><span class="hljs-keyword">FROM</span>  Student<br><span class="hljs-keyword">WHERE</span>  Sdept<span class="hljs-operator">=</span> <span class="hljs-string">&#x27;IS&#x27;</span>  <span class="hljs-keyword">AND</span>  Sage<span class="hljs-operator">&lt;</span><span class="hljs-number">20</span>；<br></code></pre></td></tr></table></figure>

<p><strong>有些情况下，视图消解法不能生成正确查询</strong></p>
<p>在 S_G 视图中查询平均成绩在 90 分以上的学生学号和平均成绩</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span><br><span class="hljs-keyword">FROM</span>   S_G<br><span class="hljs-keyword">WHERE</span>  Gavg<span class="hljs-operator">&gt;=</span><span class="hljs-number">90</span>；<br>       S_G视图的子查询定义：<br>         <span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">VIEW</span> S_G (Sno，Gavg)<br>         <span class="hljs-keyword">AS</span><br><span class="hljs-keyword">SELECT</span>  Sno，<span class="hljs-built_in">AVG</span>(Grade)<br><span class="hljs-keyword">FROM</span>  SC<br><span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> Sno；<br></code></pre></td></tr></table></figure>

<p>错误：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> Sno，<span class="hljs-built_in">AVG</span>(Grade)<br><span class="hljs-keyword">FROM</span>     SC<br><span class="hljs-keyword">WHERE</span>  <span class="hljs-built_in">AVG</span>(Grade)<span class="hljs-operator">&gt;=</span><span class="hljs-number">90</span><br><span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> Sno；<br></code></pre></td></tr></table></figure>

<p>正确：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span>  Sno，<span class="hljs-built_in">AVG</span>(Grade)<br><span class="hljs-keyword">FROM</span>  SC<br><span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> Sno<br><span class="hljs-keyword">HAVING</span> <span class="hljs-built_in">AVG</span>(Grade)<span class="hljs-operator">&gt;=</span><span class="hljs-number">90</span>；<br></code></pre></td></tr></table></figure>

<h5 id="4-4-更新视图"><a href="#4-4-更新视图" class="headerlink" title="4.4 更新视图"></a>4.4 更新视图</h5><p>更新视图和更新基本表相同</p>
<p>注：一些视图是不可更新的——当对视图的更新无法转换成对基本表 SC 的更新时，如修改平均成绩视图中某个学生的平均成绩不可实现<br><strong>举例：</strong></p>
<p><strong>向信息系学生视图 IS_S 中插入一个新的学生记录：200215129，赵新，20 岁</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">INSERT</span><br><span class="hljs-keyword">INTO</span> IS_Student<br><span class="hljs-keyword">VALUES</span>(‘<span class="hljs-number">95029</span>’，‘赵新’，<span class="hljs-number">20</span>)；<br></code></pre></td></tr></table></figure>

<p>转换为对基本表的更新：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">INSERT</span><br><span class="hljs-keyword">INTO</span>   Student(Sno，Sname，Sage，Sdept)<br><span class="hljs-keyword">VALUES</span>(‘<span class="hljs-number">200215129</span> <span class="hljs-string">&#x27;，&#x27;</span>赵新<span class="hljs-string">&#x27;，20，&#x27;</span><span class="hljs-keyword">IS</span><span class="hljs-string">&#x27; )；</span><br></code></pre></td></tr></table></figure>

<p><strong>删除信息系学生视图 IS_Student 中学号为 200215129 的记录</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DELETE</span><br><span class="hljs-keyword">FROM</span> IS_Student<br><span class="hljs-keyword">WHERE</span> Sno<span class="hljs-operator">=</span> <span class="hljs-string">&#x27; 200215129 &#x27;</span>；<br></code></pre></td></tr></table></figure>

<p>转换为对基本表的更新：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">DELETE</span><br><span class="hljs-keyword">FROM</span> Student<br><span class="hljs-keyword">WHERE</span> Sno<span class="hljs-operator">=</span> <span class="hljs-string">&#x27; 200215129 &#x27;</span> <span class="hljs-keyword">AND</span> Sdept<span class="hljs-operator">=</span> <span class="hljs-string">&#x27;IS&#x27;</span>；<br></code></pre></td></tr></table></figure>

<h6 id="更新视图的限制："><a href="#更新视图的限制：" class="headerlink" title="更新视图的限制："></a>更新视图的限制：</h6><p>一些视图是不可更新的，因为对这些视图的更新不能唯一地有意义地转换成对相应基本表的更新</p>
<p>例：视图 S_G 为不可更新视图。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">UPDATE</span>  S_G<br><span class="hljs-keyword">SET</span>          Gavg<span class="hljs-operator">=</span><span class="hljs-number">90</span><br><span class="hljs-keyword">WHERE</span>  Sno<span class="hljs-operator">=</span> ‘<span class="hljs-number">200215121</span>’；<br></code></pre></td></tr></table></figure>

<p>这个对视图的更新无法转换成对基本表 SC 的更新<br><strong>实际系统对视图更新的限制</strong></p>
<p>DB2 对视图更新的限制：<br>(1) 若视图是由两个以上基本表导出的，则此视图不允许更新。<br>(2) 若视图的字段来自字段表达式或常数，则不允许对此视图执行 INSERT 和 UPDATE 操作，但允许执行 DELETE 操作。<br>(3) 若视图的字段来自集函数，则此视图不允许更新。<br>(4) 若视图定义中含有 GROUP BY 子句，则此视图不允许更新。<br>(5) 若视图定义中含有 DISTINCT 短语，则此视图不允许更新。<br>(6) 若视图定义中有嵌套查询，并且内层查询的 FROM 子句中涉及的表也是导出该视图的基本表，则此视图不允许更新。<br>(7) 一个不允许更新的视图上定义的视图也不允许更新</p>
<p><img src="https://img-blog.csdnimg.cn/20200315190124498.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzYyNzExOA==,size_16,color_FFFFFF,t_70" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><font size=0.5>别吝啬一个 👍。<br><font size=0.5>别吝啬一个 👍。<br><font size=0.5>别吝啬一个 👍。</p>
<br>
<br>
<br>

<blockquote>
<p><strong>写在最后：</strong><br>我叫风骨散人，名字的意思是我多想可以不低头的自由生活，可现实却不是这样。家境贫寒，总得向这个世界低头，所以我一直在奋斗，想<code>改变我的命运</code>给亲人好的生活，希望<code>同样被生活绑架的你</code>可以通过自己的努力改变现状，深知成年人的世界里没有容易二字。目前是一名在校大学生，预计考研，热爱编程，热爱技术，喜欢分享，知识无界，希望我的分享可以帮到你！<br>如果有什么想看的，可以私信我，如果在能力范围内，我会发布相应的博文！<br>谢谢大家的阅读！😘</p>
</blockquote>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" class="category-chain-item">数据库</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>『数据库』震惊，某博主为吸引眼球拿出压箱底SQL总结，如果你没看那就吃亏了！（超级详细的SQL基础，你还不会的话就别学数据库了）</div>
      <div>http://example.com/2023/12/06/『数据库』震惊，某博主为吸引眼球拿出压箱底SQL总结，如果你没看那就吃亏了！（超级详细的SQL基础，你还不会的话就别学数据库了）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Chiam</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年12月6日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2023/12/06/%E3%80%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E3%80%8F%E7%BA%BF%E6%80%A7%E8%A1%A8%E9%93%BE%E5%BC%8F%E5%AD%98%E5%82%A8(%E9%93%BE%E8%A1%A8)-%E5%8D%95%E9%93%BE%E8%A1%A8/" title="『数据结构』线性表链式存储(链表)-单链表">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">『数据结构』线性表链式存储(链表)-单链表</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2023/12/06/%E3%80%8E%E6%95%B0%E6%8D%AE%E5%BA%93%E3%80%8F%E8%BF%99%E7%AF%87%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%96%87%E7%AB%A0%E7%9C%9F%E6%B2%A1%E4%BA%BA%E7%9C%8B-%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AE%8C%E6%95%B4%E6%80%A7/" title="『数据库』这篇数据库的文章真没人看-数据库完整性">
                        <span class="hidden-mobile">『数据库』这篇数据库的文章真没人看-数据库完整性</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"fIfc7WqUDZohlQuPc2lz5mJy-MdYXbMMI","appKey":"zjlAG3ZA3o4cBHVAkjzc2Z20","path":"window.location.pathname","placeholder":"留言仅限讨论，禁止广告等行为","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"https://fifc7wqu.api.lncldglobal.com","emojiCDN":null,"emojiMaps":null,"enableQQ":false},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <meta name="referrer" content="no-referrer" /> <footer id="footer" role="contentinfo"> <div class="divider"> <div class="wall"></div> <img class="animals" src="/img/footer_animals_new.png" srcset="/img/loading.gif" lazyload alt="Footer Animals"> </div> <div class="container" data-index="450"> <p> <a href="https://chiamzhang.github.io" target="_blank">DogEgg</a> <i class="iconfont icon-love"></i> <a href="#" target="_blank">LittePig</a> </p> <p> Powered by  <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-pen"></i> Theme  <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> </p> </div> </footer> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>




  
<script src="/js/love.js"></script>
<script src="/js/funnyTitle.js"></script>
<script src="/js/backTop.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/wanko.model.json"},"display":{"position":"left","width":150,"height":150,"hOffset":20,"vOffset":0},"mobile":{"show":false,"scale":0.5},"react":{"opacity":0.9},"log":false});</script></body>
</html>
